﻿// -----------------------------------------------------------------------
// <copyright file="If.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace CFScript.Control
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class If:ControlInstruction
    {
        public If()
        {
            this.name = "<if>";

            this.parameterNames = new string[] {"test"};
            this.parameterExpandable = new bool[] {true};
            this.parameterRequired = new bool[] {true};
            this.parameters = new Dictionary<string, Parameter>();
            this.hasNestedInstructions = true;

            this.condition = new BooleanExpression();

            Debug.Assert(this.parameterNames.Length == this.parameterRequired.Length &&
                         this.parameterRequired.Length == this.parameterRequired.Length);
        }

        public override InstructionResult Execute(CFScript context)
        {
            InstructionResult result = new InstructionResult();
            result.ExecutedInstruction = this;

            if (this.condition.Evaluate())
            {
                foreach (Instruction i in this.nestedInstructions)
                {
                    i.Execute(context);
                }
            }

            return result;
        }
    }
}
